package com.ibm.spatiotemp.server.datasources;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.utils.JSonInterface;

public class ClassReflectionToolkit {
	
	public static Logger logger = Logger.getLogger(ClassReflectionToolkit.class);
	
	public static Map<String,Object> getInstancesFields(Object instance)
	{
		Map<String,Object> para_val_map = new HashMap<String, Object>();

		Class<?> classobj = instance.getClass();
        Field fields[] = classobj.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
            
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getMethodName = "get" + firstLetter + fieldName.substring(1);
            
            // 获得和属性对应的getXXX()方法
            Method getMethod;
            Object value = null;
			try {
				getMethod = classobj.getMethod(getMethodName, new Class[]{});
				value = getMethod.invoke(instance, new Object[]{});
			}
				catch (IllegalAccessException e) {
//					e.printStackTrace();
				} catch (IllegalArgumentException e) {
//					e.printStackTrace();
				} catch (InvocationTargetException e) {
//					e.printStackTrace();
				}
				catch (NoSuchMethodException e) {
//					e.printStackTrace();
				} catch (SecurityException e) {
//					e.printStackTrace();
				}
			if(value != null){
				para_val_map.put(fieldName, value);
			}
        }
		return para_val_map;
	}
	
	public static String mapToJsonString(Map<String,Object> mapinstance){
		StringBuffer strbuf = new StringBuffer();
		Map<String,Object> map = mapinstance;
		for( Map.Entry<String,Object> entry: map.entrySet() ){
			if(entry.getValue() instanceof JSonInterface)
			strbuf.append(String.format("\"%s\":%s,", entry.getKey(),entry.getValue().toString()));
			else
			strbuf.append(String.format("\"%s\":\"%s\",", entry.getKey(),entry.getValue().toString()));
		}
		int strlen = strbuf.toString().length();
		String ret = strbuf.toString().substring(0, strlen-1);
		return String.format("{%s}", ret);
	}
	
	public static String[] getClassFields(Class<?> classType) throws Exception 
	{
        Field fields[] = classType.getDeclaredFields();
        int fieldlen = fields.length;
        String[] fieldstrings = new String[fieldlen];
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
            fieldstrings[i] = fieldName;
        }
		return fieldstrings;
	}
	
	   public static Object copy(Class<?> classType) throws Exception {
	        // 获得对象的类型
//	        Class<?> classType = object.getClass();
	        System.out.println("Class:" + classType.getName());
	 
	        // 通过默认构造方法创建一个新的对象
	        Object objectCopy = classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
	 
	        // 获得对象的所有属性
	        Field fields[] = classType.getDeclaredFields();
	 
	        for (int i = 0; i < fields.length; i++) {
	            Field field = fields[i];
	 
	            String fieldName = field.getName();
	            System.out.println("name====="+fieldName);
	            String firstLetter = fieldName.substring(0, 1).toUpperCase();
	            // 获得和属性对应的getXXX()方法的名字
	            String getMethodName = "get" + firstLetter + fieldName.substring(1);
	            // 获得和属性对应的setXXX()方法的名字
	            String setMethodName = "set" + firstLetter + fieldName.substring(1);
	 
	            // 获得和属性对应的getXXX()方法
//	            Method getMethod = classType.getMethod(getMethodName, new Class[]{});
	            // 获得和属性对应的setXXX()方法
//	            Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
	 
	            // 调用原对象的getXXX()方法
//	            Object value = getMethod.invoke(object, new Object[]{});
//	            System.out.println("value===="+value);
//	            System.out.println(fieldName + ":" + value);
	            // 调用拷贝对象的setXXX()方法
//	            setMethod.invoke(objectCopy, new Object[]{value});
	        }
	        return objectCopy;
	    }
	 
	    public static void main(String[] args) throws Exception {
	    	String[] res = getClassFields(DataSchema.class);
	    	
	    	for (String string : res) {
	    		logger.info(string);
			}
	    	
	    }
}
